#!/usr/bin/env python
# -*- coding:utf-8 -*-

import xml.dom.minidom
import urllib2
import hashlib

e2cTranslation = {
  "doc" : u"文件",
  "point": u"分數",
  "nihun" : u"年份",
  "hothau" : u"代號",
  "fullname" : u"全名",
  "class" : u"分類",
  "exam" : u"考試類別",
  "category" : u"種類",
  "subject" : u"科目",
  "question" : u"題目",
  "index" : u"題號",
  "figure" : u"圖片",
  "thauhang" : u"內文",
  "question-set" : u"題集",
}

def getName(name):
  return e2cTranslation[name]

def getElementHTMLStr(element):
  HTMLStr = u''
  if ((len(element.childNodes) == 1) and (element.childNodes[0].nodeType == xml.dom.Node.TEXT_NODE)):
    if element.nodeName == "figure":
      imgHTMLStr = u'<img src="/image?id=%s" alt="%s" /><br />' \
                     % (urllib2.quote(element.childNodes[0].nodeValue), element.childNodes[0].nodeValue)
      HTMLStr += u'<fieldset style="margin: .5em; border: 1px solid lime;"><legend>%s</legend>%s</fieldset>' \
                   % (getName(element.nodeName), imgHTMLStr)
    else:
      HTMLStr += u"<span style='color: red; margin: .5em;'>%s: </span>" % getName(element.nodeName)
      HTMLStr += u"<span style='color: blue'>%s</span><br />" % element.childNodes[0].nodeValue
  elif (len(element.childNodes) == 0 and element.nodeType == xml.dom.Node.TEXT_NODE):
    text = u''.join(element.nodeValue.split())
    if len(text) != 0:
      HTMLStr += u"<span style='color: blue; margin: .5em;'>%s</span><br />" % text
    return HTMLStr
  else:
    if element.nodeName == "question" or element.nodeName == "question-set":
      HTMLStr += "<br style='line-height: 1em;'>"
    legendHTMLStr = u"<legend style='color: red; font-size: 1.5em;'>%s: </legend>" % getName(element.nodeName)
    elementsHTMLStr = u''
    for child in element.childNodes:
      elementsHTMLStr += getElementHTMLStr(child)
    HTMLStr += u'<fieldset style="margin: .5em; border: 1px solid lime;">%s %s</fieldset>' % (legendHTMLStr, elementsHTMLStr)

  return HTMLStr

def parse(xmlString):
  dom = xml.dom.minidom.parseString(xmlString)
  doc = dom.getElementsByTagName('doc')[0]

  HTMLStr = getElementHTMLStr(doc)

  return HTMLStr

def getId(xmlString):
  dom = xml.dom.minidom.parseString(xmlString)
  nihun_element = dom.getElementsByTagName('nihun')[0]
  hothau_element = dom.getElementsByTagName('hothau')[0]
  indexElem = dom.getElementsByTagName('index')[0]

  Id = u''
  Id += nihun_element.childNodes[0].nodeValue
  Id += u'_moex_'
  Id += hothau_element.childNodes[0].nodeValue
  Id += u'_'
  Id += indexElem.childNodes[0].nodeValue

  # FIXME: remove white spaces (\s) in id
  return Id
def generate_tebok_ID(nihun, hothau, toeho, thauhang):
  hash_value = hashlib.sha512()
  if (nihun == None or hothau == None or toeho == None):
    if (thauhang == None):
      return 0
    else :
      return make_temp_tebok_ID(thauhang)
  else:
    ID = u''
    ID += nihun
    ID += u'_moex_'
    ID += hothau
    ID += u'_'
    ID += toeho
    hash_value.update(ID)
    hash_value.hexdigest()
    # return hash_value
    return ID

def make_temp_tebok_ID(thauhang_string):
  hash_value = hashlib.sha512()
  hash_value.update(thauhang_string)
  hash_value.hexdigest()
  ID = u''
  ID += hash_value
  return ID

if __name__ == '__main__':
  parse(open('xml/moex100060_23560.xml', 'r').read())
